package com.yaozheng.vocationaltraining.activity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.HundredQuestionsEncyclopediaAdapter;
import com.yaozheng.vocationaltraining.adapter.NewsListAdapter;
import com.yaozheng.vocationaltraining.adapter.PoWoDictionaryItemAdapter;
import com.yaozheng.vocationaltraining.app.VocationalTrainingApplication;
import com.yaozheng.vocationaltraining.bean.CategoriesBean;
import com.yaozheng.vocationaltraining.bean.CategoryItemBean;
import com.yaozheng.vocationaltraining.bean.HundredQuestionsEncyclopediaItemBean;
import com.yaozheng.vocationaltraining.bean.StatisticsBean;
import com.yaozheng.vocationaltraining.bean.UserInfoBean;
import com.yaozheng.vocationaltraining.iview.IHundredQuestionsEncyclopediaView;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.IPoWoDictionaryView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.HundredQuestionsEncyclopediaService;
import com.yaozheng.vocationaltraining.service.impl.HundredQuestionsEncyclopediaServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 百问百答
 * 
 * @author Administrator
 * 
 */
@EActivity(R.layout.activity_hundred_questions_encyclopedia)
public class HundredQuestionsEncyclopediaActivity extends BaseActivity
		implements SwipeRefreshLayout.OnRefreshListener, ILoadMoreView,
		IHundredQuestionsEncyclopediaView {

	@ViewById
	TextView headRightText;
	@ViewById
	ListView listView;
	@ViewById
	LinearLayout type_contain_layout;
	@ViewById
	CheckBox click_checkbox;
	@ViewById
	CheckBox lastest_checkbox;
	@ViewById
	CheckBox reward_checkbox;
	@ViewById
	CheckBox reply_checkbox;
	@ViewById
	CheckBox praise_checkbox;
	@ViewById
	EditText key_edittext;
	@ViewById
	ImageView search_imageview;

	@ViewById
	Button add_question_button;


	@ViewById
	RadioGroup radiogroup;
	@ViewById
	RadioButton uncomplete_radiobutton;
	@ViewById
	RadioButton already_complete_radiobutton;
	@ViewById
	RadioButton uncheck_radiobutton;

	@ViewById
	LinearLayout click_tip_layout;
	@ViewById
	TextView click_number_textview;

	@ViewById
	LinearLayout point_tip_layout;
	@ViewById
	TextView point_number_textview;

	@ViewById
	LinearLayout reply_tip_layout;
	@ViewById
	TextView reply_number_textview;

	@ViewById
	LinearLayout already_complete_tip_layout;
	@ViewById
	TextView already_complete_number_textview;

	@ViewById
	LinearLayout un_complete_tip_layout;
	@ViewById
	TextView un_complete_number_textview;


	

	/**
	 * 下拉刷新
	 */
	@ViewById
	SwipeRefreshLayout swipeRefreshLayout;

	ListViewLoadMoreManage listViewLoadMoreManage;
	
	@Bean(HundredQuestionsEncyclopediaServiceImpl.class)
	HundredQuestionsEncyclopediaService hundredQuestionsEncyclopediaService;

	HundredQuestionsEncyclopediaAdapter hundredQuestionsEncyclopediaAdapter;
	ArrayList<HundredQuestionsEncyclopediaItemBean> hundredQuestionsEncyclopediaItemData = new ArrayList<>();

	int lastId;
	int resolved = 0;//可选值为 0:未解决的。、1:解决的
	int status = 1;  //默认为 1 已审核。可用值仅限 0、1。
	String sortStr = "score";//score、answer、like、click 和 id。
	//默认为 score 悬赏分数。可用值仅限 score、answer、like、click 和 id。

	UserInfoBean userInfo;

	int categoriyId;
	List<CategoriesBean> list;
	LayoutInflater inflater;

	@AfterViews
	public void initView() {
		setHeadTitle("百问百答");
		inflater = this.getLayoutInflater();

		headRightText.setText(this.getResources().getText(R.string.myStr));

		hundredQuestionsEncyclopediaService.init(this);
		listViewLoadMoreManage = new ListViewLoadMoreManage(this, this,
				listView);
		swipeRefreshLayout.setOnRefreshListener(this);
//		swipeRefreshLayout.postDelayed(new Runnable() {
//			@Override
//			public void run() {
//				swipeRefreshLayout.setRefreshing(true);
//
//			}
//		}, 500);

		loadCategoriesData();
		loadStaticData();
		loadUserInfo();


		key_edittext
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {
					public boolean onEditorAction(TextView v, int actionId,
												  KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_SEND
								|| (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
//让mPasswordEdit获取输入焦点
							if(!TextUtils.isEmpty(key_edittext.getEditableText().toString())){
								onRefresh();
							}
							return true;
						}
						return false;
					}
				});
		search_imageview.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if(!TextUtils.isEmpty(key_edittext.getEditableText().toString())){
					onRefresh();
				}
			}
		});

		radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
				switch (i){
					case R.id.uncomplete_radiobutton:
						resolved = 0;
						status = 1;
						onRefresh();
						break;
					case R.id.already_complete_radiobutton:
						resolved = 1;
						status = 1;
						onRefresh();
						break;
					case R.id.uncheck_radiobutton:
						resolved = -1;
						status = 0;
						onRefresh();
						break;
				}
			}
		});

		click_checkbox.setOnClickListener(listener);
		lastest_checkbox.setOnClickListener(listener);
		reply_checkbox.setOnClickListener(listener);
		praise_checkbox.setOnClickListener(listener);
		reward_checkbox.setOnClickListener(listener);

		hundredQuestionsEncyclopediaAdapter = new HundredQuestionsEncyclopediaAdapter(this,
				hundredQuestionsEncyclopediaItemData);
		listView.setAdapter(hundredQuestionsEncyclopediaAdapter);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				Intent intent = new Intent();
				intent.putExtra("questionId",hundredQuestionsEncyclopediaItemData.get(i).getId());
				if (userInfo != null){
					intent.putExtra("uid",userInfo.getId());
					intent.putExtra("usertype",userInfo.getUserType());
				}
				intent.setClassName(HundredQuestionsEncyclopediaActivity.this,
						Constants.QUESTION_DETAIL_ACTIVITY_CLASS_NAME);
				HundredQuestionsEncyclopediaActivity.this.startActivity(intent);
			}
		});


		headRightText.setVisibility(View.VISIBLE);

		//onRefresh();


	}

	@Override
	protected void onResume() {
		super.onResume();
		if(userInfo != null){
			onRefresh();
		}
	}

	private void loadUserInfo() {
		hundredQuestionsEncyclopediaService.getUserInfo();
	}

	private View.OnClickListener listener = new View.OnClickListener(){
		@Override
		public void onClick(View view) {
			switch (view.getId()){
				case R.id.click_checkbox:
					sortStr = "click";
					if(lastest_checkbox.getCompoundDrawables()[2] != null){
						lastest_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reward_checkbox.getCompoundDrawables()[2] != null){
						reward_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(praise_checkbox.getCompoundDrawables()[2] != null){
						praise_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reply_checkbox.getCompoundDrawables()[2] != null){
						reply_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(click_checkbox.getCompoundDrawables()[2] != null){
//                    lastest_checkbox.setCompoundDrawables(null,null,null,null);
					}else{
						click_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,
								HundredQuestionsEncyclopediaActivity.this.getResources().
										getDrawable(R.drawable.checkbox_sort_right_bg),null);
					}

					refreshSortStatus(true);
					break;
				case R.id.lastest_checkbox:
					sortStr = "id";

					if(click_checkbox.getCompoundDrawables()[2] != null){
						click_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reward_checkbox.getCompoundDrawables()[2] != null){
						reward_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(praise_checkbox.getCompoundDrawables()[2] != null){
						praise_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reply_checkbox.getCompoundDrawables()[2] != null){
						reply_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(lastest_checkbox.getCompoundDrawables()[2] != null){
//                    lastest_checkbox.setCompoundDrawables(null,null,null,null);
					}else{
						lastest_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,
								HundredQuestionsEncyclopediaActivity.this.getResources().
										getDrawable(R.drawable.checkbox_sort_right_bg),null);
					}
					refreshSortStatus(true);
					break;
				case R.id.reward_checkbox:
					sortStr = "score";
					if(click_checkbox.getCompoundDrawables()[2] != null){
						click_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(lastest_checkbox.getCompoundDrawables()[2] != null){
						lastest_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(praise_checkbox.getCompoundDrawables()[2] != null){
						praise_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reply_checkbox.getCompoundDrawables()[2] != null){
						reply_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reward_checkbox.getCompoundDrawables()[2] != null){
//                    lastest_checkbox.setCompoundDrawables(null,null,null,null);
					}else{
						reward_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,
								HundredQuestionsEncyclopediaActivity.this.getResources().
										getDrawable(R.drawable.checkbox_sort_right_bg),null);
					}
					refreshSortStatus(true);
					break;
				case R.id.praise_checkbox:
					sortStr = "like";
					if(click_checkbox.getCompoundDrawables()[2] != null){
						click_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(lastest_checkbox.getCompoundDrawables()[2] != null){
						lastest_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reward_checkbox.getCompoundDrawables()[2] != null){
						reward_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reply_checkbox.getCompoundDrawables()[2] != null){
						reply_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(praise_checkbox.getCompoundDrawables()[2] != null){
//                    lastest_checkbox.setCompoundDrawables(null,null,null,null);
					}else{
						praise_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,
								HundredQuestionsEncyclopediaActivity.this.getResources().
										getDrawable(R.drawable.checkbox_sort_right_bg),null);
					}
					refreshSortStatus(true);
					break;
				case R.id.reply_checkbox:
					sortStr = "answer";
					if(click_checkbox.getCompoundDrawables()[2] != null){
						click_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(lastest_checkbox.getCompoundDrawables()[2] != null){
						lastest_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reward_checkbox.getCompoundDrawables()[2] != null){
						reward_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(praise_checkbox.getCompoundDrawables()[2] != null){
						praise_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
					}
					if(reply_checkbox.getCompoundDrawables()[2] != null){
//                    lastest_checkbox.setCompoundDrawables(null,null,null,null);
					}else{
						reply_checkbox.setCompoundDrawablesWithIntrinsicBounds(null,null,
								HundredQuestionsEncyclopediaActivity.this.getResources().
										getDrawable(R.drawable.checkbox_sort_right_bg),null);
					}
					refreshSortStatus(true);
					break;
			}
		}
	};

	private void loadStaticData() {
		hundredQuestionsEncyclopediaService.loadStatic();
	}

	private void refreshSortStatus() {
		refreshSortStatus(false);
	}
	private void refreshSortStatus(boolean httpFlag) {//score、answer、like、click 和 id。
		if("click".equals(sortStr)){//表示按关注排序
			click_checkbox.setBackgroundColor(this.getResources().getColor(R.color.footer_bg_color));
			lastest_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reply_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			praise_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reward_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));

			click_checkbox.setTextColor(this.getResources().getColor(R.color.default_bg_color));
			lastest_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reply_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			praise_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reward_checkbox.setTextColor(this.getResources().getColor(R.color.black));
		}else if("answer".equals(sortStr)){//表示按回复排序
			reply_checkbox.setBackgroundColor(this.getResources().getColor(R.color.footer_bg_color));
			lastest_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			click_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			praise_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reward_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));

			reply_checkbox.setTextColor(this.getResources().getColor(R.color.default_bg_color));
			lastest_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			click_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			praise_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reward_checkbox.setTextColor(this.getResources().getColor(R.color.black));
		}else if("like".equals(sortStr)){//表示按点赞排序
			praise_checkbox.setBackgroundColor(this.getResources().getColor(R.color.footer_bg_color));
			lastest_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			click_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reply_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reward_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));

			praise_checkbox.setTextColor(this.getResources().getColor(R.color.default_bg_color));
			lastest_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			click_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reply_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reward_checkbox.setTextColor(this.getResources().getColor(R.color.black));
		}else if("score".equals(sortStr)){//表示按点赞排序
			reward_checkbox.setBackgroundColor(this.getResources().getColor(R.color.footer_bg_color));
			lastest_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			click_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reply_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			praise_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));

			reward_checkbox.setTextColor(this.getResources().getColor(R.color.default_bg_color));
			lastest_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			click_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reply_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			praise_checkbox.setTextColor(this.getResources().getColor(R.color.black));
		}else if("id".equals(sortStr)){//表示按最新排序
			lastest_checkbox.setBackgroundColor(this.getResources().getColor(R.color.footer_bg_color));
			reward_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			click_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			reply_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));
			praise_checkbox.setBackgroundColor(this.getResources().getColor(R.color.default_bg_color));

			lastest_checkbox.setTextColor(this.getResources().getColor(R.color.default_bg_color));
			reward_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			click_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			reply_checkbox.setTextColor(this.getResources().getColor(R.color.black));
			praise_checkbox.setTextColor(this.getResources().getColor(R.color.black));
		}
		if(httpFlag){
			onRefresh();
		}

	}

	private void loadCategoriesData() {
		hundredQuestionsEncyclopediaService.loadCategories();
	}

	/**
	 * 保存我的
	 */
	@Click(R.id.headRightText)
	public void headRightClick() {

		Intent intent = new Intent();
		intent.putExtra("cateId",categoriyId);
		if (userInfo != null){
			intent.putExtra("uid",userInfo.getId());
			intent.putExtra("usertype",userInfo.getUserType());
		}
		intent.setClassName(this,
				Constants.ME_QUESTION_ENCYCLOPEDIA_ACTIVITY_CLASS_NAME);
		this.startActivity(intent);

	}

	@Click(R.id.add_question_button)
	public void addQuestion(){
		Intent intent = new Intent();
		intent.setClassName(this,
				Constants.ADD_QUESTION_ACTIVITY_CLASS_NAME);
		this.startActivity(intent);
	}

	@Override
	public void loadMoreData() {
		if(hundredQuestionsEncyclopediaItemData != null && hundredQuestionsEncyclopediaItemData.size() > 0){
			int lastId = hundredQuestionsEncyclopediaItemData.get(hundredQuestionsEncyclopediaItemData.size() -1).getId();
			if (lastId > 0) {
				this.lastId = lastId;
				loadData();
			}
		}
	}

	@Override
	public void onRefresh() {
		lastId = 0;
		loadData();

	}

	public void loadData() {
		String sort = "desc";
		if("click".equals(sortStr)){//表示按关注排序
			if(click_checkbox.isChecked()){
				sort = "desc";
			}else{
				sort = "asc";
			}
		}else if("answer".equals(sortStr)){//表示按回答排序
			if(reply_checkbox.isChecked()){
				sort = "desc";
			}else{
				sort = "asc";
			}
		}else if("like".equals(sortStr)){//表示按点赞排序
			if(praise_checkbox.isChecked()){
				sort = "desc";
			}else{
				sort = "asc";
			}
		}else if("score".equals(sortStr)){//表示按悬赏排序
			if(reward_checkbox.isChecked()){
				sort = "desc";
			}else{
				sort = "asc";
			}
		}else if("id".equals(sortStr)){//表示按最新排序
			if(lastest_checkbox.isChecked()){
				sort = "desc";
			}else{
				sort = "asc";
			}
		}
		hundredQuestionsEncyclopediaService.list(lastId,categoriyId,
				key_edittext.getEditableText().toString().trim(),sortStr,sort,resolved,status);
	}

	@UiThread
	@Override
	public void responseSuccess(JSONObject jsonObject) {
		try {
			cancelLoadDataProgressDialog();
			JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","questions");
			if (dataList != null) {
				if (lastId == 0) {
					hundredQuestionsEncyclopediaItemData.clear();
				}

				for (int i = 0; i < dataList.size(); i++) {
					JSONObject jsonObject2 = dataList.getJSONObject(i);

					HundredQuestionsEncyclopediaItemBean bean = new HundredQuestionsEncyclopediaItemBean();
					bean.setId(jsonObject2.getInt("id"));
					bean.setUid(jsonObject2.getInt("uid"));
					bean.setCateId(jsonObject2.getInt("cateId"));
					bean.setCateName(jsonObject2.getString("cateName"));
					bean.setTitle(jsonObject2.getString("title"));
					bean.setAnswer(jsonObject2.getInt("answer"));
					bean.setScore(jsonObject2.getInt("score"));
					bean.setLike(jsonObject2.getInt("like"));
					bean.setClick(jsonObject2.getInt("click"));
					if(jsonObject2.containsKey("resolved")){
						bean.setResolved(jsonObject2.getInt("resolved"));
					}else {
						bean.setResolved(-1);
					}


					bean.setCreatedTime(jsonObject2.getString("createdTime"));

					hundredQuestionsEncyclopediaItemData.add(bean);
				}

				if(userInfo != null){
					hundredQuestionsEncyclopediaAdapter.setData(hundredQuestionsEncyclopediaItemData,resolved,userInfo.getId());
				}else{
					hundredQuestionsEncyclopediaAdapter.setData(hundredQuestionsEncyclopediaItemData,resolved,-1);
				}
				hundredQuestionsEncyclopediaAdapter.notifyDataSetChanged();
			}
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
		}
	}

	@Override
	public void cancelLoadDataProgressDialog() {
		swipeRefreshLayout.setRefreshing(false);
		listViewLoadMoreManage.loadMoreComplete();
	}

	@UiThread
	@Override
	public void responseError(String error) {
		cancelLoadDataProgressDialog();
		alertMessage(error);
	}

	@Override
	public void categoriesListSuccess(JSONObject jsonObject) {
		LogUtils.println(jsonObject.toString());

		JSONArray categoriesList = TypeUtils.getJsonArray(jsonObject,"data",
				"categories");

		list = new ArrayList<>();
		CategoriesBean allBean = new CategoriesBean();
		allBean.setId(0);
		allBean.setActiveIcon(null);
		allBean.setIcon(null);
		allBean.setName("全部问答");
		list.add(allBean);

		for (int i = 0; i < categoriesList.size(); i++) {
			JSONObject jsonObject2 = categoriesList.getJSONObject(i);

			CategoriesBean bean = new CategoriesBean();
			bean.setId(jsonObject2.getInt("id"));
			bean.setActiveIcon(jsonObject2.getString("activeIcon"));
			bean.setIcon(jsonObject2.getString("icon"));
			bean.setName(jsonObject2.getString("name"));
			list.add(bean);
		}

		if(list != null && list.size() > 0){
			this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					for(int i= 0 ; i < list.size() ; i++){
						CategoriesBean bean = list.get(i);
						View dictionaryItemLayout = inflater.inflate(R.layout.dictionary_item
								, null);
						dictionaryItemLayout.setTag(bean.getId());
						ImageView iconImageView = (ImageView) dictionaryItemLayout.findViewById(R.id.icon_imageview);
						TextView nameTextView = (TextView) dictionaryItemLayout.findViewById(R.id.name_textview);

						nameTextView.setText(bean.getName());


						if(categoriyId == bean.getId()){
							if(i == 0){
								iconImageView.setImageResource(R.drawable.icon_study_class_all_now);
							}else{
								Glide.with(HundredQuestionsEncyclopediaActivity.this.getApplicationContext())
										.load(bean.getActiveIcon())
										.crossFade()
										.into(iconImageView);
							}
							nameTextView.setTextColor(HundredQuestionsEncyclopediaActivity.this.getResources().getColor(R.color.footer_bg_color));
						}else{
							if(i == 0){
								iconImageView.setImageResource(R.drawable.icon_study_class_all);
							}else{
								Glide.with(HundredQuestionsEncyclopediaActivity.this.getApplicationContext())
										.load(bean.getIcon())
										.crossFade()
										.into(iconImageView);
							}
							nameTextView.setTextColor(Color.BLACK);
						}

						dictionaryItemLayout.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View view) {
								categoriyId = (int) view.getTag();

								for(int i = 0 ; i < type_contain_layout.getChildCount() ; i++){
									View childView = type_contain_layout.getChildAt(i);
									if(childView.getTag().toString().equals(view.getTag().toString())){
										if(categoriyId == 0){
											((ImageView) childView.findViewById(R.id.icon_imageview)).setImageResource(R.drawable.icon_study_class_all_now);
										}else{
											Glide.with(HundredQuestionsEncyclopediaActivity.this.getApplicationContext())
													.load(list.get(i).getActiveIcon())
													.crossFade()
													.into((ImageView) childView.findViewById(R.id.icon_imageview));
										}
										((TextView) childView.findViewById(R.id.name_textview)).setTextColor(HundredQuestionsEncyclopediaActivity.this.getResources().getColor(R.color.footer_bg_color));
									}else{
										if(i == 0){
											((ImageView) childView.findViewById(R.id.icon_imageview)).setImageResource(R.drawable.icon_study_class_all);
										}else{
											Glide.with(HundredQuestionsEncyclopediaActivity.this.getApplicationContext())
													.load(list.get(i).getIcon())
													.crossFade()
													.into((ImageView) childView.findViewById(R.id.icon_imageview));
										}
										((TextView) childView.findViewById(R.id.name_textview)).setTextColor(Color.BLACK);
									}
								}

								onRefresh();
							}
						});
						type_contain_layout.addView(dictionaryItemLayout);
					}


				}
			});
		}


		onRefresh();

	}

	@Override
	public void categoriesListError(String error) {
		LogUtils.println(error);
	}

	@Override
	public void statisticsSuccess(JSONObject jsonObject) {
		JSONObject statistics = TypeUtils.getJsonObject(jsonObject,"data",
				"statistics");
		final StatisticsBean statisticsBean = new StatisticsBean();
		statisticsBean.setAnswerCnt(statistics.getInt("answerCnt"));
		statisticsBean.setClickCnt(statistics.getInt("clickCnt"));
		statisticsBean.setResolved(statistics.getInt("resolved"));
		statisticsBean.setScoreCnt(statistics.getInt("scoreCnt"));
		statisticsBean.setUnresolved(statistics.getInt("unresolved"));

		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(statisticsBean.getClickCnt() < 1){
					click_tip_layout.setVisibility(View.GONE);
				}else{
					click_tip_layout.setVisibility(View.VISIBLE);
					click_number_textview.setText(statisticsBean.getClickCnt()+"");
				}
				if(statisticsBean.getAnswerCnt() < 1){
					reply_tip_layout.setVisibility(View.GONE);
				}else{
					reply_tip_layout.setVisibility(View.VISIBLE);
					reply_number_textview.setText(statisticsBean.getAnswerCnt()+"");
				}
				if(statisticsBean.getResolved() < 1){
					already_complete_tip_layout.setVisibility(View.GONE);
				}else{
					already_complete_tip_layout.setVisibility(View.VISIBLE);
					already_complete_number_textview.setText(statisticsBean.getResolved()+"");
				}
				if(statisticsBean.getScoreCnt() < 1){
					point_tip_layout.setVisibility(View.GONE);
				}else{
					point_tip_layout.setVisibility(View.VISIBLE);
					point_number_textview.setText(statisticsBean.getScoreCnt()+"");
				}
				if(statisticsBean.getUnresolved() < 1){
					un_complete_tip_layout.setVisibility(View.GONE);
				}else{
					un_complete_tip_layout.setVisibility(View.VISIBLE);
					un_complete_number_textview.setText(statisticsBean.getUnresolved()+"");
				}

			}
		});
	}

	@Override
	public void statisticsError(String error) {

	}

	@UiThread
	@Override
	public void userInfoSuccess(JSONObject jsonObject) {
		JSONObject dataJsonObject = TypeUtils.getJsonObject(jsonObject, "data","user");
		userInfo = new UserInfoBean();
		userInfo.setId(dataJsonObject.getInt("id"));
		userInfo.setUserType(dataJsonObject.getInt("userType"));
		//TODO 消息没有填充完毕

		if(userInfo.getUserType() == 2){ //经销商才显示提问按钮
			add_question_button.setVisibility(View.VISIBLE);
		}else{
			add_question_button.setVisibility(View.GONE);
		}

		//add_question_button.setVisibility(View.VISIBLE); // for test TODO

		if(userInfo.getUserType() != 1){ // 管理员才显示待审核
			if(radiogroup.getChildCount() == 3){
				radiogroup.removeViewAt(2);
			}
		}

		refreshSortStatus();


	}

	@Override
	public void userInfoError(String serverConnectionError) {

	}
}
